﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace GZ.Entity
{
    /// <summary>
    /// 配送时段实体类
    /// </summary>
    public class EDeliveryPeriod
    {
        #region 属性
        public EDeliveryPeriod()
        {
            this.deliveryperiodname = string.Empty;
            this.endtime = null;
        }

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string deliveryperiodname;

        public string DeliveryPeriodName
        {
            get { return deliveryperiodname; }
            set { deliveryperiodname = value; }
        }

        private DateTime? endtime;

        public DateTime? EndTime
        {
            get { return endtime; }
            set { endtime = value; }
        }
        private int isdelete;
        public int IsDelete
        {
            get { return isdelete; }
            set
            {
                isdelete = value;
            }
        }
        #endregion

        #region

        /// <summary>
        /// 获取所有配送时段
        /// </summary>
        /// <returns>配送时段列表</returns>



        public DataTable GetDeliveryPeriod()
        {
            string sql = "SELECT * FROM tb_deliveryperiod WHERE IsDelete=0 ORDER BY EndTime ";
            DataTable dt = MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql).Tables[0];
            return dt;
        }

        /// <summary>
        /// 根据配送时段Id得到配送时段集合
        /// </summary>
        /// <param name="Id">配送时段Id</param>
        /// <returns>配送时段对象集合</returns>
        public IList<EDeliveryPeriod> GetDeliveryPeriodById(int Id)
        {
            string sql = "SELECT * FROM tb_deliveryperiod WHERE Id = @Id";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@Id", Id);
            return GZ.Common.ModelConvertHelper<EDeliveryPeriod>.ConvertToModel(MySqlHelper.ExecuteDataset(GZ.DAL.MySQLHelper.connStr, sql, parameters).Tables[0]);

        }

        /// <summary>
        /// 判断配送日期是否有效
        /// </summary>
        /// <param name="SelectedDate">选择的日期</param>
        /// <returns>有效返回True，无效返回False</returns>
        public bool IsEffective(DateTime SelectedDate, int EDeliveryPeriodId)
        {
            if (SelectedDate.Date > DateTime.Now.Date)
            {
                return true;
            }
            else if (SelectedDate.Date == DateTime.Now.Date)
            {
                IList<EDeliveryPeriod> list = GetDeliveryPeriodById(EDeliveryPeriodId);
                foreach (EDeliveryPeriod var in list)
                {
                    if (DateTime.Now.TimeOfDay < var.EndTime.Value.TimeOfDay)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 添加配送时段
        /// </summary>
        /// <param name="obj">配送时段对象</param>
        /// <returns>成功返回True,失败返回False</returns>
        public bool Insert(EDeliveryPeriod obj)
        {
            obj.IsDelete = 0;
            string sql = @"INSERT INTO tb_deliveryperiod
                        ( 
                        DeliveryPeriodName,
                        EndTime,
                        IsDelete)
                        VALUES
                        ( 
                        @DeliveryPeriodName,
                        @EndTime,
                        @IsDelete
                        );";
            MySqlParameter[] parameters = new MySqlParameter[3];
            parameters[0] = new MySqlParameter("@DeliveryPeriodName", obj.DeliveryPeriodName);
            parameters[1] = new MySqlParameter("@EndTime", obj.EndTime);
            parameters[2] = new MySqlParameter("@IsDelete", obj.IsDelete);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

        /// <summary>
        /// 根据Id删除配送周期
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Del(int Id)
        {
            string sql = "Update tb_deliveryperiod SET IsDelete = 1 WHERE Id=@Id";
            MySqlParameter[] parameters = new MySqlParameter[1];
            parameters[0] = new MySqlParameter("@Id", Id);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

        /// <summary>
        /// 根据Id修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Modify(EDeliveryPeriod obj)
        {
            string sql = @"UPDATE tb_deliveryperiod
                            SET 
                            DeliveryPeriodName = @DeliveryPeriodName,
                            EndTime = @EndTime,
                            IsDelete = @IsDelete
                            WHERE Id=@Id;";
            MySqlParameter[] parameters = new MySqlParameter[4];
            parameters[0] = new MySqlParameter("@DeliveryPeriodName", obj.DeliveryPeriodName);
            parameters[1] = new MySqlParameter("@EndTime", obj.EndTime);
            parameters[2] = new MySqlParameter("@IsDelete", obj.IsDelete);
            parameters[3] = new MySqlParameter("@Id", obj.Id);
            return MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters) > 0 ? true : false;
        }

        #endregion
    }
}
